class Solution {
public:
    int subarraySum(vector<int>& nums, int k) {
        unordered_map<int,int> dp;
        int ret=0;
        int sum=0;
        for(auto& x:nums)
        {
            sum+=x;
            if(dp.count(sum-k))
            {
                ret+=dp[sum-k];
            }
            dp[sum]++;
            if(sum==k)
            {
                ret++;
            }
        }
        return ret;
    }
};
